package com.zl.test.restdemo.controller;

import com.zl.test.restdemo.entity.MiddleStudent;
import com.zl.test.restdemo.exception.DemoServiceException;
import com.zl.test.restdemo.service.MiddleStudentService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import com.zl.test.restdemo.common.utils.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @Author: zhang lei
 * @Describe: All things are difficult before they are easy.
 * @CreatTime: 2021-02-24-16-25
 */
@RestController
@RequestMapping(value = "/stu")
@Api(value = "学生")
@Slf4j
public class MiddleStudentController {
    @Autowired
    private MiddleStudentService studentService;
    @GetMapping(value = "/quertStuByName")
    @ApiOperation("通过名称查询学生信息")
    public ResponseEntity quertStuByName(@RequestParam(value = "name") String name) {
        if (StringUtils.isBlank(name)) {
            log.error("参数不可为空");
            throw new DemoServiceException("8989", "错误");
        } else if ("123".equals(name)) {
            log.error("参数不可为空");
            throw new DemoServiceException("参数不可为123", "111", name);
        }
        MiddleStudent student = null;
        try {
            student = studentService.quertStuByName(name);
            return  ResponseEntity.success(student);
        }catch (DemoServiceException demoServiceException){
            log.error("quertStuByName 服务异常"+demoServiceException.getMessage());
            return ResponseEntity.failure(demoServiceException);
        }catch (Exception e) {
            log.error("quertStuByName 系统异常 {}",e);
            return ResponseEntity.failure("quertStuByName 系统异常");
        }
    }
    @GetMapping(value = "/getStuListByRedis")
    @ApiOperation("查询学生列表,并添加缓存")
    public ResponseEntity getStuList() {
        List<MiddleStudent> students = studentService.selectAll();
        return  ResponseEntity.success(students);
    }

}
